package com.neusoft.neuiotms.dcom.converter.insurance;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.insurance.InsuranceSummaryTaskDTO;
import com.neusoft.neuiotms.dcom.model.enums.InsuranceTaskStatus;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceSummaryTask;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceSummaryTaskFiles;
import com.neusoft.neuiotms.dcom.repository.insurance.InsuranceSummaryTaskFilesRepository;
import com.neusoft.neuiotms.dcom.repository.insurance.InsuranceSummaryTaskRepository;

@Component
public class InsuranceSummaryTaskConverter
        extends AbstractConverter<InsuranceSummaryTask, InsuranceSummaryTaskDTO> {

    @Autowired
    private BaseIdGenerator insuranceSummaryTaskCodeGenerator;
    @Autowired
    private InsuranceSummaryTaskRepository summaryRepository;
    @Autowired
    private InsuranceSummaryTaskFilesRepository filesRepository;
    @Autowired
    private InsuranceSummaryTaskFilesConverter filesConverter;

    @Override
    public InsuranceSummaryTask toModel(final InsuranceSummaryTaskDTO dto) {

        InsuranceSummaryTask model;
        if (dto.isNew()) {
            model = new InsuranceSummaryTask();
            if (StringUtils.isEmpty(dto.getCode())) {
                model.setCode(this.insuranceSummaryTaskCodeGenerator.generateCode());
                model.setUnitCode(dto.getUnitCode());
                model.setStartTime(dto.getStartTime());
                model.setEndTime(dto.getEndTime());
            } else {
                model.setCode(dto.getCode());
            }
        } else {
            model = this.summaryRepository.getOne(dto.getId());
        }

        model.setStatus(InsuranceTaskStatus.valueOf(dto.getStatus()));
        model.setRecord(dto.getRecord());
        model.setReportPersonCode(dto.getReportPersonCode());
        model.setReportPersonName(dto.getReportPersonName());

        // 附件内容表
        final List<InsuranceSummaryTaskFiles> files = model.getSummaryTaskFiles().stream().collect(Collectors.toList());

        files.forEach(k -> {
            model.getSummaryTaskFiles().remove(k);
            this.filesRepository.delete(k);
        });

        dto.getFiles().stream().map(s -> {
            final InsuranceSummaryTaskFiles file = this.filesConverter.toModel(s);
            file.setSummaryTask(model);
            return file;
        }).forEach(model.getSummaryTaskFiles()::add);

        return model;
    }

    @Override
    public InsuranceSummaryTaskDTO toDTO(final InsuranceSummaryTask model, final boolean forListView) {

        final InsuranceSummaryTaskDTO dto = new InsuranceSummaryTaskDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setReportPersonCode(model.getReportPersonCode());
        dto.setReportPersonName(model.getReportPersonName());
        dto.setUnitCode(model.getUnitCode());
        dto.setStartTime(model.getStartTime());
        dto.setEndTime(model.getEndTime());
        dto.setStatus(model.getStatus().name());
        dto.setRecord(model.getRecord());
        // 附件内容
        model.getSummaryTaskFiles().stream().map(s -> this.filesConverter.toDTO(s)).filter(k -> null != k)
                .forEach(dto.getFiles()::add);

        return dto;
    }

}
